#!/bin/bash
# Version:1.0.0
# Author:modongxiao
# Description:php install
# chmod +x php-install.sh

# 错误监测
# 参数（在执行后执行函数，并传入错误信息）
# 第一个参数：固定 $?
# 第二个参数：错误信息
# 例如：check_error $? "执行错误"
function check_error {
     if [ $1 -ne 0 ] ; then
        echo $2",请检查！";
        exit 1
    fi
    return 0;
}

case "$1" in
    install)
        # 初始化准备与配置
        if [ -d "/usr/local/php5" ] ; then
            echo "php可能已经安装，请查看 /usr/local/ 目录下的 php 目录";
            exit 1
        fi
        if [ ! -d "/data/log/php" ] ; then
            mkdir -pv /data/log/php/
            echo "日志目录创建完成";
        fi
        if [ ! -d "/data/webapps/" ] ; then
            mkdir -pv /data/webapps/
            echo "webapps目录创建完成";
        fi

        # 依赖包安装
        mkdir php
        cd php
        yum -y install wget tar xz
        yum -y install gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt libmcrypt-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl perl-devel psmisc.x86_64 recode recode-devel libtidy libtidy-devel
        check_error $? "yum 安装依赖包错误"

        # 下载 与 编译安装
        wget http://172.18.202.1/source/php/php-5.3.28.tar.bz2
        check_error $? "安装包下载错误"
        tar -axf php-5.3.28.tar.bz2
        cd php-5.3.28/
        check_error $? "安装包路径错误"
         ./configure --prefix=/usr/local/php5 --sysconfdir=/etc/php5 --with-config-file-path=/etc/php5 --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mhash --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib --enable-mbstring --with-mcrypt --enable-sockets --with-iconv-dir --with-xsl --enable-zip --with-pcre-dir --with-pear --enable-session  --enable-gd-native-ttf --enable-xml --with-freetype-dir --enable-gd-jis-conv --enable-inline-optimization --enable-shared --enable-bcmath --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mbregex --enable-pcntl --with-xmlrpc --with-gettext --enable-exif --with-readline --with-recode --with-tidy
        check_error $? "configure 编译错误"
        make -j 4
        check_error $? "make 编译错误"
        make install
        check_error $? "make install 编译错误"

        echo "编译安装完成...开始配置...";
        # php 配置
        echo "export PATH=/usr/local/php5/sbin:$PATH" > /etc/profile.d/php5.sh;
        source /etc/profile.d/php5.sh
        mkdir /usr/local/php5/extension
        wget http://172.18.202.1/source/php/conf/php.ini -P /etc/php5/
        wget http://172.18.202.1/source/php/conf/php-fpm.conf -P /etc/php5/
        wget http://172.18.202.1/source/php/conf/ZendGuardLoader.so -P /usr/local/php5/extension/
        mkdir /data/webapps/magedu -pv
        [ ! -f /etc/rc.d/init.d/php-fpm ] && wget http://172.18.202.1/source/php/conf/php-fpm -P /etc/rc.d/init.d/
        chmod +x /etc/rc.d/init.d/php-fpm
        chkconfig --add php-fpm
        chkconfig --level 345 php-fpm on

        # echo "<?php phpinfo();" > /data/webapps/magedu/index.php
        # 启动
        service php-fpm start
        check_error $? "启动完成 ！ "
        exit 0
    ;;
    delete)
        service php-fpm stop
        rm -rf /usr/local/php5
        rm -rf /etc/php5
        rm -rf /etc/rc.d/init.d/nginx
        echo '卸载完成 ! '
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {install|delete|}'
        exit 1
    ;;
esac